@extends('admin.layout.layout')
@section('title','分类列表')
@section('con')
<div id="page-wrapper" style="min-height: 370px;">
	<div class="row">
	    <div class="col-lg-12 page-header">
	        <h1>分类列表</h1>
	    </div>
	    <!-- /.col-lg-12 -->
	</div>

	<!-- 分类列表 -->
	<div class="row">
	    <div class="col-lg-12">
	        <div class="panel panel-default">
	            <div class="panel-heading">
	                分类列表
	            </div>
	            <!-- /.panel-heading -->
	            <div class="panel-body">
	                <div class="dataTable_wrapper">
	                    <div id="dataTables-example_wrapper" class="dataTables_wrapper form-inline dt-bootstrap no-footer">
	                    	<div class="row">
		                    	<div class="col-sm-6">
		                    		<div class="dataTables_length" id="dataTables-example_length">
		                    			<!-- 每页显示多少条  搜索 -->
		                    		</div>
		                    	</div>
		                	</div>
		                	<div class="row">
		                		<div class="col-sm-12">
		                			<table id="dataTables-example" class="table table-striped table-bordered table-hover dataTable no-footer" role="grid" aria-describedby="dataTables-example_info">
				                        <thead>
				                            <tr role="row">
				                            	<th>分类ID</th>
				                            	<th>类名</th>
				                            	<th>分类图片</th>
				                            	<th>父类</th>
				                            	<th>路径</th>
				                            	<th>操作</th>
				                            </tr>
				                        </thead>
				                        <tbody>
											@foreach($cates as $k=>$v)
					                        <tr class="gradeA odd" role="row">
				                                <td class="center id" width="20%">{{$v->cid}}</td>
				                                <td class="center">{{$v->cname}}</td>
				                                <td class="center"><img src="{{asset($v->cpic)}}" alt=""></td>
				                                <td class="center">{{$v->pname}}</td>
				                                <td class="center">{{$v->path}}</td>
				                                <td class="center" width="20%">
				                                	<a href="/admin/cate/edit?id={{$v->cid}}" class="btn btn-outline btn-primary" type="button">修改</a>
				                                	<button class="btn btn-outline btn-danger del" type="button">删除</button>
				                                </td>
				                            </tr>
				                            @endforeach
			                            </tbody>
	                   				</table>
	                   			</div>
	                   		</div>

	                   		<!-- 分页 -->
	                   		<div class="row">
	                   			<div style="margin:0px auto">
	                   				<!-- 分页 -->
	                   			</div>
	                   		</div>
	                   	</div>
	                </div>
	            </div>
	        </div>
	    </div>
	</div>
</div>
@endsection
@section('js')
	<script type="text/javascript">
		// 解决post发送请求时 CSRF保护问题  两步: 1.meta头 csrf  2. $ajaxSetup
		$.ajaxSetup({
	        headers: {
	            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
	        }
	    });

		// 绑定单击事件 删除 -  修改
		$('.del').click(function(){
			var id = $(this).parents('tr').find('.id').html();  //获取ID
			var btn = $(this);

			$.post('/admin/cate/del',{cid:id},function(data){
				if(data == 3){
					alert('删除成功');
					btn.parents('tr').remove();
				}else if(data == 2){
					alert('删除失败,该分类下有商品');
				}else{
					alert('删除失败,该分类下有子分类');
				}
			})
		})
	</script>
@endsection